//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

package uxgenes.ext.camera;

import uxgenes.core.Logger;
import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


//
// Camera Preview Surface
//
public final class CameraSurfacePreview 	extends 	SurfaceView
											implements 	SurfaceHolder.Callback,
														ICameraSurface,
														Camera.PreviewCallback
{
// constants //
	private static final String	TAG = "CameraSurfacePreview";
	
	
// variables //
    private final SurfaceHolder 	_surfaceHolder;
    private boolean					_surfaceCreated	= false;
	private UXCamera				_camera 		= null;
    
    
// methods //
    
    // SurfaceView //
	public CameraSurfacePreview (Context context)
	{
		super(context);
		_surfaceHolder = this.getHolder();
		_surfaceHolder.addCallback( this );
	}

	public CameraSurfacePreview (Context context, AttributeSet attrs)
	{
		super(context, attrs);
		_surfaceHolder = this.getHolder();
		_surfaceHolder.addCallback( this );
	}
	
	public CameraSurfacePreview (Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		_surfaceHolder = this.getHolder();
		_surfaceHolder.addCallback( this );
	}

	
// SurfaceHolder.Callback //
	
	// surfaceChanged
	public final void surfaceChanged (SurfaceHolder holder, int format, int width, int height)
	{}
	
	// surfaceCreated
	public final void surfaceCreated (SurfaceHolder holder)
	{
		_surfaceCreated = true;
	}

	// surfaceDestroyed
	public final void surfaceDestroyed (SurfaceHolder holder)
	{
		_surfaceCreated = false;
	}
	
	
// ICameraSurface //
	
	// IsCreated
	public final boolean IsCreated ()
	{
		return _surfaceCreated;
	}

	
	// SetCamera
	public final void SetCamera (UXCamera cam)
	{
		if ( _camera == cam )
			return;
		
		if ( cam == null && _camera != null ) {
			_surfaceHolder.removeCallback( _camera );
		}
		
		_camera = cam;
		
		if ( _camera != null ) {
			_surfaceHolder.addCallback( _camera );
		}
	}

	
	// SetToPreview
	public final void OnStartPreview ()
	{
		if ( _camera == null || _camera.GetHwCamera() == null )
			return;
		
		try {
			_camera.GetHwCamera().setPreviewDisplay( _surfaceHolder );
			_camera.GetHwCamera().setPreviewCallback( this );
		}
		catch (Exception e) {
			Logger.OnCatchException( TAG, e );
		}
	}
	
	
	// OnStopPreview
	public final void OnStopPreview ()
	{
		if ( _camera == null || _camera.GetHwCamera() == null )
			return;
		
		try {
			_camera.GetHwCamera().setPreviewDisplay( null );
			_camera.GetHwCamera().setPreviewCallback( null );
		}
		catch (Exception e) {
			Logger.OnCatchException( TAG, e );
		}
	}
	
	
// Camera.PreviewCallback //
	
	// onPreviewFrame
	public final void onPreviewFrame (byte[] data, Camera camera)
	{
		if ( _camera != null ) {
			_camera.OnPreviewFrame( data, camera );
		}
	}

}
